/**
 * 城市头部的封装
 * @author hongwei 2018-09-07
 */
(function (window) {
    'use strict';
    var applyCityHeader = function(params) {
        var that = this;
        this.extend(that.params, params);

        that.initHeader();      //step1:初始化头部
        that.archiveBtn();      //step2:点击城市确定
    };
    applyCityHeader.prototype = {
        //参数
        params : {
            cityType        : 'single' //城市类型,single是单城市,multiply是多城市
            ,frameName      : ''   //框架名称：'select_city_many'
            ,storageName    : ''   //storage名称
            ,eventName      : ''   //事件名称
        }

        //step1:初始化头部
        ,initHeader : function () {
            var that = this;

            initHeader(that.params.frameName);
        }

        //step2:点击城市确定
        ,archiveBtn : function () {
            var that = this;
            $('#achieve').click(function(){
                if(!that.validateConfirm()){
                    return false;
                }
                var cName = $api.getStorage(that.params.storageName).replace(/\s+/g, ' ');
                api.sendEvent({
                    name: that.params.eventName,
                    extra: {
                        cname:cName
                    }
                });
                api.closeWin();
            });
        }

        //step3:验证确认城市
        , validateConfirm: function (){
            var that = this;

            //step1:选择的城市
            var cityName = $api.getStorage(that.params.storageName);
            var citySelectArray = cityName.split(" ");

            //step2:所有城市
            var allCityArray = $api.getStorage('all_cities');

            //step3:只允许5个城市
            if(citySelectArray.length > 5){
                toast.custom({
                    title:"城市最多5个",
                    html:'<i class="aui-iconfont aui-icon-info"></i>',
                    duration:2000
                });
                return false;
            }

            //step4:所选城市是否在所有城市中
            var judge = that.isContained(allCityArray, citySelectArray);
            if (judge === false) {
                alert('你选择的城市不在城市列表中！');
                return false;
            }
            return true;
        }

        /**
         * 工具1:函数:扩展
         * @param a
         * @param b
         * @returns {*}
         */
        ,extend: function (a, b) {
            for (var key in b) {
                if (b.hasOwnProperty(key)) {
                    a[key] = b[key];
                }
            }
            return a;
        }

        /**
         * 工具2：js判断一个数组是否包含另一个数组
         * @author hongwei 2018-09-07
         * @param aa 例如：[1,2,5,7,3]
         * @param bb 例如：[1,7,2]
         * @returns {boolean}
         */
        ,isContained :function(aa,bb) {
            if (!(aa instanceof Array) || !(bb instanceof Array) || ((aa.length < bb.length))) {
                return false;
            }
            var aaStr = aa.toString();
            for (var i = 0; i < bb.length; i++) {
                if (aaStr.indexOf(bb[i]) < 0) return false;
            }
            return true;
        }

    };
    window.applyCityHeader =  applyCityHeader;
})(window);